package com.sky.controller.admin;

import com.sky.dto.OrdersCancelDTO;
import com.sky.dto.OrdersPageQueryDTO;
import com.sky.dto.OrdersRejectionDTO;
import com.sky.dto.OrdersSubmitDTO;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.OrderService;
import com.sky.vo.OrderStatisticsVO;
import com.sky.vo.OrderSubmitVO;
import com.sky.vo.OrderVO;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Slf4j
@RestController
@RequestMapping("admin/order")
public class OrderController {

    @Autowired
    private OrderService orderService;

    /**
     * 取消订单
     *
     * @return
     */
    @PutMapping("cancel")
    public Result cancelOrder(@RequestBody OrdersCancelDTO ordersCancelDTO) {
        log.info("取消订单:{}", ordersCancelDTO);
        orderService.cancelOrder(ordersCancelDTO);
        return Result.success();
    }

    /**
     * 拒单
     *
     * @return
     */
    @PutMapping("rejection")
    public Result rejectionOrder(@RequestBody OrdersRejectionDTO ordersRejectionDTO) {
        log.info("拒绝订单");
        orderService.rejectionOrder(ordersRejectionDTO);
        return Result.success();
    }

    /**
     * 完成订单
     *
     * @param id
     * @return
     */
    @PutMapping("complete/{id}")
    public Result completeOrder(@PathVariable Integer id) {
        log.info("完成订单:{}", id);
        orderService.completeOrder(id);
        return Result.success();
    }

    /**
     * 接单
     *
     * @param id
     * @return
     */
    @PutMapping("confirm")
    public Result confirmOrder(@RequestBody Integer id) {
        log.info("接单");
        orderService.confirmOrder(id);
        return Result.success();
    }

    /**
     * 派送订单
     *
     * @param id
     * @return
     */
    @PutMapping("delivery/{id}")
    public Result deliveryOrder(@PathVariable Integer id) {
        log.info("派送订单:{}", id);
        orderService.deliveryOrder(id);
        return Result.success();
    }

    /**
     * 查询订单详情
     *
     * @param id
     * @return
     */
    @GetMapping("details/{id}")
    public Result<OrderVO> findOrderDetails(@PathVariable Integer id) {
        log.info("查询订单详情:{}", id);
        OrderVO orderVO = orderService.findOrderDetails(id);
        return Result.success(orderVO);
    }

    /**
     * 订单搜索
     *
     * @return
     */
    @GetMapping("conditionSearch")
    public Result findOrderSearch(OrdersPageQueryDTO ordersPageQueryDTO) {
        log.info("订单搜索:{}", ordersPageQueryDTO);
        PageResult pageResult = orderService.findOrderSearch(ordersPageQueryDTO);
        return Result.success(pageResult);
    }

    /**
     * 各个状态的订单数量统计
     *
     * @return
     */
    @GetMapping("statistics")
    public Result findOrderStatistics() {
        log.info("各个状态的订单数量统计");
        OrderStatisticsVO orderStatisticsVO = orderService.findOrderStatistics();
        return Result.success(orderStatisticsVO);
    }

    /**
     * 用户下单
     *
     * @param ordersSubmitDTO
     * @return
     */
    @PostMapping("/submit")
    public Result<OrderSubmitVO> submit(@RequestBody OrdersSubmitDTO ordersSubmitDTO) {
        log.info("用户下单：{}", ordersSubmitDTO);
        OrderSubmitVO orderSubmitVO = orderService.submitOrder(ordersSubmitDTO);
        return Result.success(orderSubmitVO);
    }

}